from django.shortcuts import render
import json
# Create your views here.

from django.db.models import Max, Sum, Count, Q, Func, F
from django.db.models import F, ExpressionWrapper, FloatField, Func
from django.shortcuts import render
from rest_framework import viewsets,mixins

import json
from django.core import serializers
from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.http import require_http_methods

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views import View
from django.db import connection

from apptest.models import DataHeat,DataLine,DataTotal,DataPosition,IndexRound,DataCongestionPoint

class IndexView(View):
    def get(self, request, *args, **kwargs):
        time = self.request.GET.get('tabs_time_1', '7')             #获取下拉框的值time,默认值7
        street = self.request.GET.get('tabs_st_1','1')
        time2 = self.request.GET.get('tabs_time_2','7')
        street2 = self.request.GET.get('tabs_st_2','1')

        # 汉字数字字典
        digit_dict = {'1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六', '7': '七', '8': '八',
                      '9': '九','10':'十'}
        context = {}                #返回给HTML的字典

        timepie = time.split('.')[0]
        streetpie = '1'
        if(street == '2'): streetpie = street

        timepheat = time.split('.')[0]
        if(time == '0') : qs_dataheat = DataHeat.objects.filter(timetype = '7').values('id','num','x','y')
        else : qs_dataheat = DataHeat.objects.filter(timetype = timepheat).values('id','num','x','y')
        dataheat = []
        for i in qs_dataheat:
            dataheat.append(json.dumps({'name': '坐标' + i['id'], 'num': i['num'], 'x': i['x'], 'y': i['y']}))
        context['dataheat'] = dataheat                      #将热力图数据存入字典

        #时间轴
        data_x_default = ['7:00', '7:30', '8:00', '8:30', '9:00', '9:30', '10:00', '10:30', '11:00',
                '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00']
        data_y_default = [176, 123, 253, 279, 330, 263, 233, 226, 252, 264, 299, 289, 278, 231, 238, 207, 253, 223, 231]
        qs_dataline = DataLine.objects.filter(street = street).values('num').order_by('id')
        data_y = []
        for i in qs_dataline:
            data_y.append(i['num'])
        context['datatimeaxis'] = {'datax':data_x_default,'datay':data_y}

        if(street == '0'): streetrt = '1'
        else: streetrt = street
        if(time == '0'): timert = '7'
        else: timert = time
        qs_datart = DataTotal.objects.filter(street = streetrt ,time = timert).values('num','stop_num','avg_velocity','avg_dist')
        context['datart'] = qs_datart

        dataradar = []
        qs_dataradar = DataTotal.objects.filter(time = timert).values('avg_velocity').order_by('street')
        for i in qs_dataradar:
            dataradar.append(float(i['avg_velocity'])*10+40)
        context['dataradar'] = dataradar


        qs_position = DataPosition.objects.values('px','py').filter(timetype = time)
        dataposition = []
        for i in qs_position:
            dataxy = []
            dataxy.append(i['px'])
            dataxy.append(i['py'])
            dataposition.append(dataxy)
        context['dataposition'] = dataposition

##########################功能二#############################################
        qs_datatotal = DataTotal.objects.values('num','stop_num','avg_dist').filter(time=time).order_by('street')
        datatotal = []
        for i in qs_datatotal:
            data = []
            data.append(i['num'])
            data.append(i['stop_num'])
            data.append(i['avg_dist'])
            datatotal.append(data)
        context['datatotal'] = datatotal


##########################功能三#############################################
        timebar = time2.split('.')[0]
        qs_index = IndexRound.objects.values('index_round').filter(time = timebar).order_by('street')
        databar = []
        for i in qs_index:
            databar.append(i['index_round'])
        context['databar'] = databar

        qs_congestionpoint = DataCongestionPoint.objects.values('x','y','num').filter(timetype = timebar)
        datacongestionpoints = []
        for i in qs_congestionpoint:
            datapoint = []
            datapoint.append(i['x'])
            datapoint.append(i['y'])
            datapoint.append(i['num'])
            datacongestionpoints.append(datapoint)
        context['datacongestionpoints'] = datacongestionpoints



        #sts = ['一','二','三','四','五','六','七','八','九','十']
        sts = range(1,11)
        context['sts'] = sts             #将下拉框的路段范围和时间范围存入字典，便于显示
        return render(request, 'index.html', context)


